package com.budwk.app.sqfwV2.component;

import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.component.CommonComponent;
import com.budwk.app.sqfw.constants.SqfwFormSummaryConstants;
import com.budwk.app.sqfw.enums.SupportBusinessTypeEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwDownRecordlogService;
import com.budwk.app.sqfw.vo.GenerateExcelUrlVO;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseExcelParamDTO;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseTemplateExcelGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.ExcelGeneratorConfig;
import com.budwk.app.sqfwDoc.codeGenerator.impl.SqfwTemplateExcelCommonService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwV2.dto.dto.FireSafetyOnDayingRoadPdfDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownExcelLedgerReqVo;
import com.budwk.app.sqfwV2.enums.DayingRoadFileServiceEnum;
import com.budwk.app.sys.services.SysDictService;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@IocBean
public class FireSafetyOnDayingRoadExcelService extends SqfwTemplateExcelCommonService {

    @Inject
    private SysDictService dictService;
    @Inject
    private SqfwDownRecordlogService sqfwDownRecordlogService;

    @Override
    public List<GenerateExcelUrlVO> generateExcel(List<ExcelCreateConfig> excelConfigList, String userId, List<String> sqfwIds) {
        return super.generateExcel(excelConfigList, userId, sqfwIds);
    }

    public List<GenerateExcelUrlVO> downExcelLedger(DownExcelLedgerReqVo reqVo, List<ExcelCreateConfig> excelConfigList, String userId, List<Sqfw_formSummary_new> list) {
        // 1 检查是否已经下载
        List<GenerateExcelUrlVO> res = new ArrayList<>();
        String entityIds = CommonComponent.getEntityIds(list, reqVo.getSqfwIds(), SupportBusinessTypeEnum.SAFETY_CHECK.getName());
        List<ExcelCreateConfig> createConfigs = DocumentDownComponent.checkIsDownFile(excelConfigList, userId, entityIds, res, "1");
        if (createConfigs.size() == 0) {
            return res;
        }
        List<GenerateExcelUrlVO> urlVOS = super.generateExcelCommon(createConfigs, userId, reqVo.getSqfwIds(), list);
        // 3 记录下载日志
        DocumentDownComponent.addGenerateExcel(createConfigs, urlVOS, userId, "1", entityIds);
        return urlVOS;

    }


    /**
    * 允许自定义生成excel的原材料，
    *
    * @param baseExcelParam 生成excel提供的原材料
    * @param userId         用户id
    * @param sqfwIdList     社区服务id列表
    */
    @Override
    protected void customerPopularBaseExcelParam(BaseExcelParamDTO baseExcelParam, String userId, List<String> sqfwIdList) {

    }

    @Override
    protected void customerPopularBaseExcelParamForAdmin(BaseExcelParamDTO baseExcelParam, String userId, List<String> formSummaryNewIdList) {

    }

    @Override
    protected List<ExcelGeneratorConfig> getExcelCodeGeneratorConfig(List<ExcelCreateConfig> excelConfigList, BaseExcelParamDTO baseExcelParam) {
        log.info("dayinglu excel getExcelCodeGeneratorConfig: {}", JSONObject.toJSONString(excelConfigList));
        if (excelConfigList.size() == 0) {
            return Collections.emptyList();
        }
        // 查询大营路EXCEL台账合集字典数据
        List<ExcelGeneratorConfig> list = new ArrayList<>();
        for (ExcelCreateConfig config : excelConfigList) {
            ExcelGeneratorConfig generatorConfig = new ExcelGeneratorConfig();
            DayingRoadFileServiceEnum serviceEnum = DayingRoadFileServiceEnum.getByCode(config.getFileIdentity());
            if (serviceEnum != null) {
                BaseTemplateExcelGenerator service = serviceEnum.getService();
                generatorConfig.setGenerator(service);
            }
            generatorConfig.setExcelCreateConfig(config);
            list.add(generatorConfig);
        }
        return list;
    }

    @Override
    protected AbstractCustomerPdfCreateField parseParamData(String paramDataStr) {
        return JSONObject.parseObject(paramDataStr, FireSafetyOnDayingRoadPdfDTO.class);
    }

    /**
    * 第五张表中的需要的类型，详情见 {@link SqfwFormSummaryConstants}
    *
    * @return 返回数据
    */
    @Override
    protected String getSqfwFormSummaryType() {
        return SupportBusinessTypeEnum.SAFETY_CHECK.getName();
    }
}
